package cn.lingyangwl.agile.msg.push.core.channel.client;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceHttpClientImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceJoddHttpImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceOkHttpImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.lingyangwl.agile.msg.push.api.constatnts.ChannelCodeEnum;
import cn.lingyangwl.agile.msg.push.assembly.MsgChannelParamAssembly;
import cn.lingyangwl.agile.msg.push.config.wxjava.WxMaInRedisTemplateConfigStorageConfiguration;
import cn.lingyangwl.agile.msg.push.core.channel.MsgChannel;
import cn.lingyangwl.agile.model.module.config.param.WxMaParam;
import cn.lingyangwl.framework.core.utils.spring.SpringUtils;
import com.binarywang.spring.starter.wxjava.miniapp.enums.HttpClientType;
import com.binarywang.spring.starter.wxjava.miniapp.properties.WxMaProperties;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

/**
 * 微信小程序客户端
 * @author shenguangyang
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
public class WxMaMsgClient extends BaseMsgChannelClient {
    private WxMaService wxMaService;
    private WxMaProperties wxMaProperties;

    @Override
    public ChannelCodeEnum channelCode() {
        return ChannelCodeEnum.WX_MINI_APP;
    }

    @Override
    public void init() {
        WxMaParam wxMaParam = (WxMaParam) this.getMsgChannel().getParamConfig();
        this.wxMaProperties = MsgChannelParamAssembly.INSTANCES.toWxMaProperties(wxMaParam);

        // 这里读取自动配置的值
        WxMaProperties properties = SpringUtils.getBean(WxMaProperties.class);
        WxMaProperties.ConfigStorage configStorage = properties.getConfigStorage();

        // 初始哈存储配置
        WxMaProperties.ConfigStorage currentConfigStorage = this.wxMaProperties.getConfigStorage();
        currentConfigStorage.setType(configStorage.getType());
        currentConfigStorage.setHttpClientType(configStorage.getHttpClientType());
        currentConfigStorage.setHttpProxyHost(configStorage.getHttpProxyHost());
        currentConfigStorage.setMaxRetryTimes(configStorage.getMaxRetryTimes());
        currentConfigStorage.setHttpProxyPassword(configStorage.getHttpProxyPassword());
        currentConfigStorage.setKeyPrefix(configStorage.getKeyPrefix());
        currentConfigStorage.setRetrySleepMillis(configStorage.getRetrySleepMillis());

        // 覆盖token
        this.wxMaProperties.setToken(properties.getToken());
        this.wxMaProperties.setMsgDataFormat("JSON");


        WxMaInRedisTemplateConfigStorageConfiguration storageConfiguration = new WxMaInRedisTemplateConfigStorageConfiguration(this.wxMaProperties);
        WxMaConfig wxMaConfig = storageConfiguration.wxMaConfig();

        HttpClientType httpClientType = this.wxMaProperties.getConfigStorage().getHttpClientType();
        switch (httpClientType) {
            case OkHttp:
                this.wxMaService  = new WxMaServiceOkHttpImpl();
                break;
            case JoddHttp:
                this.wxMaService  = new WxMaServiceJoddHttpImpl();
                break;
            case HttpClient:
                this.wxMaService  = new WxMaServiceHttpClientImpl();
                break;
            default:
                this.wxMaService  = new WxMaServiceImpl();
        }

        this.wxMaService.setWxMaConfig(wxMaConfig);
    }

    public WxMaMsgClient(MsgChannel msgChannel) {
        super(msgChannel);
    }
}
